import { Get, Post, Put, Delete, Controller, Query, Body, Param } from '@nestjs/common';
import { CatsService } from './cats.service';
import CreateCatDto from './dtos/create.cat.dto';
import UpdateCatDto from './dtos/update.cat.dto';
import QueryCatsDto from './dtos/query.cats.dto';
import message from '../utils/message';
import success from '../utils/success';

@Controller('cats')
export class CatsController {
  constructor(private readonly service: CatsService) {
  }

  @Post()
  async create(@Body() cat: CreateCatDto) {
    this.service.create(cat);
    return success();
  }

  @Get()
  async findAll(@Query() query: QueryCatsDto) {
    const cats = this.service.findAll();
    return success({ cats, query });
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const cat = this.service.findOne(id);
    if (!cat) {
      return message(1, 'Not Found');
    }
    return success({ cat });
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() cat: UpdateCatDto) {
    const code = this.service.update(id, cat);
    if (code) {
      return message(code, 'Update Failed');
    }
    return success({
      cat: this.service.findOne(id),
    });
  }

  @Delete(':id')
  async remove(@Param('id') id: string) {
    const code = this.service.remove(id);
    if (code) {
      return message(code, 'Remove Failed');
    }
    return success();
  }
}
